import { addClass, deleteClass, updateClass, queryClass } from '../services/classes';

export default {
  namespace: 'classes',

  state: {
    classList: [],
  },

  effects: {
    *fetchClassList({ payload = {} }, { call, put }) {
      const dataLsit = yield call(queryClass, payload);
      if (dataLsit.status) {
        yield put({
          type: 'saveClassList',
          payload: dataLsit.data,
        });
      }
    },

    *updateClassList({ payload = {} }, { call, put }) {
      const data = yield call(updateClass, payload);
      if (data.status) {
        const dataLsit = yield call(queryClass, {});
        yield put({
          type: 'saveClassList',
          payload: dataLsit.data,
        });
      }
    },

    *deleteClassList({ payload = {} }, { call, put }) {
      const data = yield call(deleteClass, payload);
      if (data.status) {
        const dataLsit = yield call(queryClass, {});
        yield put({
          type: 'saveClassList',
          payload: dataLsit.data,
        });
      }
    },

    *addClassList({ payload = {} }, { call, put }) {
      const data = yield call(addClass, payload);
      if (data.status) {
        const dataLsit = yield call(queryClass, {});
        yield put({
          type: 'saveClassList',
          payload: dataLsit.data,
        });
      }
    },
  },

  reducers: {
    saveClassList(state, { payload }) {
      return {
        ...state,
        classList: payload,
      };
    },
  },
};
